package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.ReturnType;
import com.hemaapp.yjnh.utils.RegexUtils;

/**发票信息
 * @author GuiyouZhang
 * Date：2015年12月24日 下午1:36:55
 * Email:guiyouzhang@163.com
 */
public class InvoiceActivity extends BaseActivity implements OnClickListener{

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private RadioGroup groupType;
	private RadioButton type0;
	private RadioButton type1;
	private RadioButton type2;
	private LinearLayout invoice_info;
	private EditText editor;
	private RadioGroup groupContent;
	private LinearLayout layout_email;
	private EditText ed_email;
	private Button btnOk;

	private int ten = 0;
	private ArrayList<ReturnType> types = new ArrayList<ReturnType>();//发票内容列表，这里复用退货原因
	private int invoice_type = 3;//发票类型：1纸质发票，2电子发票，3无发票
	private String invoice_head = "无";//发票抬头
	private String invoice_email = "无";//电子发票的邮箱地址
	private String invoice_content = "";//发票内容
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_invoice);
		super.onCreate(savedInstanceState);
		ten = BaseUtil.dip2px(mContext, 10);
		init();
	}
	
	private void init() {
		getNetWorker().invoiceContentList(getApplicationContext().getUser().getToken());
		if (invoice_type == 3) {
		    invoice_info.setVisibility(View.GONE);
        }else
            invoice_info.setVisibility(View.VISIBLE);
		if (!isNull(invoice_head)) {
			setDatas();
		}
	}

	/**将之前已经填写过的发票信息显示
	 * 
	 */
	private void setDatas() {
		editor.setText(invoice_head);
		if (invoice_type == 1) {
			groupType.check(R.id.type0);
		}else if (invoice_type == 2) {
			groupType.check(R.id.type1);
			ed_email.setText(invoice_email);
		}
	}

	@Override
	protected void getExras() {
		invoice_type = mIntent.getIntExtra("type", 3);
		invoice_head = mIntent.getStringExtra("head");
		invoice_email = mIntent.getStringExtra("email");
		invoice_content = mIntent.getStringExtra("content");
		super.getExras();
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		groupType = (RadioGroup)findViewById( R.id.group_type );
		type0 = (RadioButton)findViewById( R.id.type0 );
		type1 = (RadioButton)findViewById( R.id.type1 );
		type2 = (RadioButton)findViewById( R.id.type2 );
		invoice_info = (LinearLayout) findViewById(R.id.invoice_info);
		editor = (EditText)findViewById( R.id.editor );
		groupContent = (RadioGroup)findViewById( R.id.group_content );
		layout_email = (LinearLayout) findViewById(R.id.layout_email);
		ed_email = (EditText)findViewById( R.id.ed_email );
		btnOk = (Button)findViewById( R.id.btn_ok );
	}

	@Override
	protected void setListener() {
		titleLeft.setOnClickListener(this);
		titleText.setText("发票信息");
		titleRight.setVisibility(View.GONE);

		BaseUtil.setEmojiFilter(editor);
		type0.setOnClickListener( this );
		type1.setOnClickListener( this );
		type2.setOnClickListener( this );
		btnOk.setOnClickListener( this );
		groupType.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.type0:
                    invoice_info.setVisibility(View.VISIBLE);
					editor.setEnabled(true);
					layout_email.setVisibility(View.GONE);
					invoice_type = 1;
					break;
				case R.id.type1:
                    invoice_info.setVisibility(View.VISIBLE);
					editor.setEnabled(true);
					layout_email.setVisibility(View.VISIBLE);
					invoice_type = 2;
					break;
				case R.id.type2:
				    invoice_info.setVisibility(View.GONE);
					editor.setEnabled(false);
					layout_email.setVisibility(View.GONE);
					invoice_type = 3;
					break;
				default:
					break;
				}
			}
		});
		groupContent.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				for(ReturnType type:types) {
					if (type.getId().equals(checkedId+"")) {
						invoice_content = type.getname();
						return;
					}
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			finish();
			break;
		case R.id.btn_ok://提交确定
			invoice_head = editor.getText().toString();
			invoice_email = ed_email.getText().toString();
			if (invoice_type != 3) {
				if (isNull(invoice_head) || isNull(invoice_content)) {
					XtomToastUtil.showShortToast(mContext, "请输入完整信息");
					break;
				}
				if (invoice_type == 2) {
					if (isNull(invoice_email)) {
						XtomToastUtil.showShortToast(mContext, "请输入邮箱地址");
						break;
					}else if (!RegexUtils.judgeEmail(invoice_email)) {
						XtomToastUtil.showShortToast(mContext, "请输入正确邮箱地址");
						break;
					}
				}
			}
			Intent data = new Intent();
			data.putExtra("type", invoice_type);
			data.putExtra("head", invoice_head);
			data.putExtra("email", invoice_email);
			data.putExtra("content", invoice_content);
			setResult(RESULT_OK, data);
			finish();
			break;
		default:
			break;
		}
	}
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BILLS_CONTENT_LIST:
			showProgressDialog("加载中");
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BILLS_CONTENT_LIST:
			cancelProgressDialog();
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BILLS_CONTENT_LIST:
			HemaPageArrayResult<ReturnType> rResult = (HemaPageArrayResult<ReturnType>) baseResult;
			if (rResult == null || rResult.getObjects() == null)
				return;
			types.clear();
			types.addAll(rResult.getObjects());
			setInvoiceContent();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BILLS_CONTENT_LIST:
            XtomToastUtil.showShortToast(mContext, "获取数据失败,"+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case BILLS_CONTENT_LIST:
            XtomToastUtil.showShortToast(mContext, "获取数据失败，请稍后再试");
			break;
		default:
			break;
		}
	}
	
	/**设置发票内容
	 * 
	 */
	private void setInvoiceContent() {
		if (groupContent.getChildCount() > 0) {
			groupContent.removeAllViews();
		}
		for (ReturnType type : types) {
			RadioButton tempButton = (RadioButton) getLayoutInflater().inflate(R.layout.invoice_radiobtn, null);
			tempButton.setId(Integer.parseInt(type.getId()));
			tempButton.setText(type.getname());
			if (!isNull(invoice_content)
					&& invoice_content.equals(type.getname())) {
				tempButton.setChecked(true);
			}
			groupContent.addView(tempButton);
		}
	}
}
